using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace PhilWicklund.com.RSSReaderWebPart
{
    public class RSSReaderEP : EditorPart
    {
        TextBox txtXmlUrl;
        RadioButtonList lstHeadlineMode;

        protected override void CreateChildControls()
        {
            Controls.Add(new LiteralControl("Feed Url:<br />"));
            txtXmlUrl = new TextBox();
            txtXmlUrl.Width = new Unit("100%");
            txtXmlUrl.TextMode = TextBoxMode.MultiLine;
            txtXmlUrl.Rows = 3;

            if (WebPartManager.Personalization.Scope == PersonalizationScope.User)
                txtXmlUrl.Enabled = false;
            this.Controls.Add(txtXmlUrl);
            Controls.Add(new LiteralControl("Headline Model:<br />"));
            lstHeadlineMode = new RadioButtonList();
            lstHeadlineMode.Items.Add(RSSReaderWebPart.RenderMode.Full.ToString());
            lstHeadlineMode.Items.Add(RSSReaderWebPart.RenderMode.Titles.ToString());
            this.Controls.Add(lstHeadlineMode);
        }

        public override void SyncChanges()
        {
            this.EnsureChildControls();
            RSSReaderWebPart sourcePart = (RSSReaderWebPart)this.WebPartToEdit;
            string SelectedMode = sourcePart.HeadlineMode.ToString();
            lstHeadlineMode.Items.FindByText(SelectedMode).Selected = true;
            txtXmlUrl.Text = sourcePart.XmlUrl;
        }

        public override bool ApplyChanges()
        {
            this.EnsureChildControls();
            RSSReaderWebPart targetPart = (RSSReaderWebPart)this.WebPartToEdit;
            targetPart.XmlUrl = txtXmlUrl.Text;
            if (lstHeadlineMode.SelectedValue.Equals("Full"))
                targetPart.HeadlineMode = RSSReaderWebPart.RenderMode.Full;
            else
                targetPart.HeadlineMode = RSSReaderWebPart.RenderMode.Titles;
            return true;
        }
    }
}
